package cn.luckdou.WeChat.Dispatcher;



import cn.luckdou.WeChat.model.TextMessage;
import cn.luckdou.WeChat.config.WxMpConfig;
import cn.luckdou.WeChat.utils.AccessTokenUtils;
import cn.luckdou.WeChat.utils.HttpUtils;
import cn.luckdou.WeChat.utils.MessageUtil;
import cn.luckdou.commons.utils.SpringContextHolder;
import cn.luckdou.system.model.we_chat_location.WeChatLocation;
import cn.luckdou.system.service.we_chat.IWeChatService;
import cn.luckdou.system.service.we_chat_location.IWeChatLocationService;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author Dadou 2017-12-5
 * 事件消息分发处理器
 */
public class EventDispatcher {

    private static IWeChatService weChatService = SpringContextHolder.getBean(IWeChatService.class);
    private static IWeChatLocationService weChatLocationService=SpringContextHolder.getBean(IWeChatLocationService.class);

    /**
     *
     * @param map
     * @return  xml 串
     */
    public static String processEvent(Map<String, String> map, HttpServletResponse response) {
        String event = map.get("Event");
        //关注事件
        if (event.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)){
            return subscribe(map);
        }

        //取消关注事件
        if (event.equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)){
            return unsubscribe(map);
        }

        //扫描二维码事件
        if (event.equals(MessageUtil.EVENT_TYPE_SCAN)) {
            return scan(map);
        }

        //位置上报事件
        if (event.equals(MessageUtil.EVENT_TYPE_LOCATION)) {
            return location(map);
        }

        //自定义菜单点击事件
        if (event.equals(MessageUtil.EVENT_TYPE_CLICK)) {
            return click(map);
        }

        //自定义菜单View事件
        if (event.equals(MessageUtil.EVENT_TYPE_VIEW)) {
            return view(map);
        }

        return null;
    }



    /**
     * 关注事件
     * @param map
     * @return
     */
    private static String subscribe(Map<String, String> map) {
        System.out.println("==============这是关注事件！");
        String Access_token = AccessTokenUtils.getToken();
        String userInfoUrl = WxMpConfig.getUser_info_url(Access_token, map.get("FromUserName"));
        String userInfo =null;
        try{
            userInfo = HttpUtils.sendGet(userInfoUrl);
        }catch (Exception e){
            e.printStackTrace();
        }
        if (userInfo!=null){
            Integer insert = weChatService.insert(userInfo);
        }
        TextMessage textMessage=new TextMessage(map,"欢迎关注");
        return MessageUtil.textMessageToXml(textMessage);
    }

    /**
     * 取消关注事件
     * @param map
     * @return
     */
    private static String unsubscribe(Map<String, String> map) {
        System.out.println("==============这是取消关注事件！");
        String openid = map.get("FromUserName"); // 用户openid
        Integer integer=weChatService.updateToSubscribe(openid);
        return null;
    }

    /**
     * 扫描二维码事件
     * @param map
     * @return
     */
    private static String scan(Map<String, String> map) {

        System.out.println("==============这是扫描二维码事件！");
        return null;
    }

    /**
     * 位置上报事件
     * @param map
     * @return
     */
    private static String location(Map<String, String> map) {
        System.out.println("==============这是位置上报事件！");
        boolean b = weChatLocationService.insert(new WeChatLocation(map));
        return null;
    }

    /**
     * 自定义菜单点击事件
     * @param map
     * @return
     */
    private static String click(Map<String, String> map) {
        System.out.println("==============这是自定义菜单点击事件！");
        return null;
    }

    /**
     * 自定义菜单View事件
     * @param map
     * @return
     */
    private static String view(Map<String, String> map) {
        System.out.println("==============这是自定义菜单View事件！");
        return null;
    }

}


